package com.hzsd.zengbanyi.biaodan_text.Actrivity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hzsd.zengbanyi.biaodan_text.BaseActivity;
import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.bean.ProgressWebView;
import com.hzsd.zengbanyi.biaodan_text.bean.UserDAO;


public class WebActivity extends BaseActivity {
	  private ProgressWebView webview;
	  private String name,password,group,user_code,user_departid;//名字，密码，部门,用户账号,用户部门ID
	  private UserDAO userDAO;

	   private String title,url,urlPath,upLoadurlPath;
//	   private final static String LCJK_URL= "/AppClient/www/RFIDList.html";
	   private final static String LCJK_URL= "/Sreach/App_Enter.ashx";
	   private final static String lHS_URL= "/AppClient/www/Analyse.html";
	   private final static String SJJH_URL= "6010/AppModules/AnalyseCheck/App_Enter.ashx";

	    @SuppressLint("SetJavaScriptEnabled")
		@Override  
	    protected void onCreate(Bundle savedInstanceState)  
	    {  
	        super.onCreate(savedInstanceState);  
	        setContentView(R.layout.activity_web);
	        webview = (ProgressWebView) findViewById(R.id.webview); 
	        SharedPreferences sp =getSharedPreferences("user", MODE_PRIVATE);
	        urlPath=sp.getString("urlPath","");
	        upLoadurlPath=sp.getString("upLoadurlPath","");
	        group=sp.getString("group","");
			name=sp.getString("name","");
			password=sp.getString("password","");
			//查询用户账号
			userDAO=new UserDAO(WebActivity.this);
			user_code=userDAO.getLoginName(name);
			//查询用户部门ID
			user_departid=userDAO.getGroupID(name);
			
	        Intent intent = getIntent();
	        title=intent.getStringExtra("title");

	        switch (title) {
			case "样品流程监控":
				url="http://"+upLoadurlPath+LCJK_URL+"?username="+user_code+"&pwd="+password;
				break;

			case "理化室":
				url="http://"+urlPath+lHS_URL+"?"+"ip="+urlPath.substring(0, urlPath.lastIndexOf(":"))+"&user_code="+user_code+"&user_truename="+name+"&user_departid="+user_departid+"&user_departname="+group;
				//url="http://"+urlPath+lHS_URL+"?"+"user_code="+user_code+"&user_truename="+name+"&user_departid="+user_departid+"&user_departname="+group;
				//+"?"+"user_code="+user_code+"&user_truename="+name+"&user_departid="+"user_departid"+"&user_departname="+group
				break;
			case "数据校核":
				url="http://"+urlPath.substring(0, urlPath.lastIndexOf(":")+1)+SJJH_URL+"?username="+user_code+"&pwd="+password;
				break;
			}
	        WebSettings webSettings = webview.getSettings();  
//	        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
	        //设置WebView属性，能够执行Javascript脚本    
	        webSettings.setJavaScriptEnabled(true);    
	        //设置可以访问文件  
	        webSettings.setAllowFileAccess(true);  
//	        webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
	        //监听指定页面链接
	        webview.setWebViewClient(new MyWebViewClient());
	         //设置支持缩放  
	        webSettings.setBuiltInZoomControls(true); 
	        //缓存的使用（不使用缓存）
	        webview.getSettings().setCacheMode(webSettings.LOAD_NO_CACHE);
	        /***打开本地缓存提供JS调用**/  
	        webview.getSettings().setDomStorageEnabled(true);  
	           // Set cache size to 8 mb by default. should be more than enough  
	        webview.getSettings().setAppCacheMaxSize(1024*1024*8);  
	          // This next one is crazy. It's the DEFAULT location for your app's cache  
	           // But it didn't work for me without this line.  
	           // UPDATE: no hardcoded path. Thanks to Kevin Hawkins  
	           String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();  
	        webview.getSettings().setAppCachePath(appCachePath);  
	        webview.getSettings().setAllowFileAccess(true);  
	        webview.getSettings().setAppCacheEnabled(true); 
	        
	        webview.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch (event.getAction ()) {
	                case MotionEvent.ACTION_DOWN :
	                case MotionEvent.ACTION_UP :
	                    if (!v.hasFocus ()) {
	                       v.requestFocus ();
	                    }
	                    break ;
	                }
					return false;
				}
			});
	        webview.setDownloadListener(new DownloadListener() {
	            @Override
	            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
	                if (url != null && url.startsWith("http://"))
	                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
	            }
	        });
	        //加载需要显示的网页    
	        webview.loadUrl(url);
	        //设置Web视图    
//	        webview.setWebViewClient(new webViewClient ());    
//			rl.setOnClickListener(new OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
//						finish();
//				}
//			});
	    }  
	       
	  
//	    @Override  
//	    public boolean onCreateOptionsMenu(Menu menu)  
//	    {  
//	        // Inflate the menu; this adds items to the action bar if it is present.  
//	        getMenuInflater().inflate(R.menu.main, menu);  
//	        return true;  
//	    }  
//	      
	    @Override   
	    //设置回退    
	    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法    
	    public boolean onKeyDown(int keyCode, KeyEvent event) {    
	        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {    
	            webview.goBack(); //goBack()表示返回WebView的上一页面    
	            return true;    
	        }    
	        finish();//结束退出程序  
	        return false;    
	    }    
	        
//	    //Web视图    
//	    private class webViewClient extends WebViewClient {    
//	        public boolean shouldOverrideUrlLoading(WebView view, String url) {    
//	            view.loadUrl(url);    
//	            return true;    
//	        }    
//	    }    
	    private class MyWebViewClient extends WebViewClient {
	    	@Override
	    	public boolean shouldOverrideUrlLoading(WebView view, String url) {
	    	if (url != null && url.contains("AppClient/www/Nav.html")) {
//	    	Intent intent = new Intent(WebActivity.this, MainActivity.class);
//	    	WebActivity.this.startActivity(intent);
	    	 
	    	finish();
	    	return true;
	    	}  


	    	return super.shouldOverrideUrlLoading(view, url);
	    	}


			@Override
	    	public void onPageFinished(WebView view, String url) {
	    	view.getSettings().setJavaScriptEnabled(true);
	    	super.onPageFinished(view, url);


	    	}
	    	}
}